package by.ostrovsky.webhousingsystem.bean.data;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import by.ostrovsky.webhousingsystem.bean.TemplateDataBean;
import by.ostrovsky.webhousingsystem.controller.IOperateController;
import by.ostrovsky.webhousingsystem.controller.userrequest.DeleteHousingRequestController;
import by.ostrovsky.webhousingsystem.controller.userrequest.GetHousingRequestController;
import by.ostrovsky.webhousingsystem.controller.userrequest.UpdateHousingRequestController;
import by.ostrovsky.webhousingsystem.exception.CommonException;
import by.ostrovsky.webhousingsystem.exception.EmptyParameterException;
import by.ostrovsky.webhousingsystem.exception.OperationErrorException;
import by.ostrovsky.webhousingsystem.hibernate.entity.Account;
import by.ostrovsky.webhousingsystem.hibernate.entity.UserRequest;

@SuppressWarnings("serial")
public class HousingRequestBean extends TemplateDataBean<UserRequest>{
	private static Logger logger = Logger
			.getLogger(HousingRequestBean.class);
	
	private String[] accountId = null;
	private String[] deadline = null;
	private String[] work = null;
	private String[] phone = null;
	private String[] addinfo = null;
	
	private void initSelectedEntitiesList() throws OperationErrorException {
		UserRequest ur = new UserRequest();
		Account acc = new Account();
		try {
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date date = null;
			if (deadline[Integer.parseInt(selectedEllements[0])] != "")
				date = dateFormat.parse(deadline[Integer
						.parseInt(selectedEllements[0])]);
			
			ur.setId(Long.parseLong(id[Integer.parseInt(selectedEllements[0])]));
			ur.setPhone(phone[Integer.parseInt(selectedEllements[0])]);
			ur.setAddinfo(addinfo[Integer.parseInt(selectedEllements[0])]);
			acc.setId(accountId[Integer.parseInt(selectedEllements[0])]);
			ur.setAccount(acc);
			ur.setWork(work[Integer.parseInt(selectedEllements[0])]);
			ur.setDeadline(date);
			ur.setAccepted(selectedEntities.get(0).getAccepted());
			ur.setRegdate(selectedEntities.get(0).getRegdate());

			selectedEntities = new LinkedList<UserRequest>();
			selectedEntities.add(ur);
		} catch (Exception e) {
			logger.error(e);
			throw new OperationErrorException();
		}
	}
	
	public HousingRequestBean() {
		try {
			read();
		} catch (CommonException e) {
			logger.error(e);
		}
	}
	
	public void initAndRetreat(){
		initSelected();
		try {
			sessionMap.put("loadNext", "employee");
			sessionMap.put("requestBean", HousingRequestBean.this);
			String source = ServletActionContext.getRequest().getHeader("REFERER");
			ServletActionContext.getResponse().sendRedirect(source);
		} catch (IOException e) {
			logger.error(e);
		}
	}
	
	@Override
	public void delete() throws CommonException {
		initSelected();
		if(selectedEntities.isEmpty())
			throw new EmptyParameterException();
		IOperateController<UserRequest, Boolean> controller = new DeleteHousingRequestController();
		controller.delete(selectedEntities);
		read();
		try {
			sessionMap.put("reloadPrevContent", "request");
			sessionMap.put("requestBean", HousingRequestBean.this);
			String source = ServletActionContext.getRequest().getHeader("REFERER");
			ServletActionContext.getResponse().sendRedirect(source);
		} catch (IOException e) {
			logger.error(e);
		}
	}

	@Override
	public void update() throws CommonException {
		initSelected();
		if(selectedEllements == null)
			throw new EmptyParameterException();
		initSelectedEntitiesList();
		if(selectedEntities.isEmpty())
			throw new EmptyParameterException();
		IOperateController<UserRequest, Boolean> controller = new UpdateHousingRequestController();
		controller.set(selectedEntities);
		read();
		try {
			sessionMap.put("reloadPrevContent", "request");
			sessionMap.put("requestBean", HousingRequestBean.this);
			String source = ServletActionContext.getRequest().getHeader("REFERER");
			ServletActionContext.getResponse().sendRedirect(source);
		} catch (IOException e) {
			logger.error(e);
		}
	}

	@Override
	public void read() throws CommonException {
		entityList = new LinkedList<UserRequest>();
		IOperateController<UserRequest, Boolean> controller = new GetHousingRequestController();
		entityList.addAll(controller.get());
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public String[] getAccountId() {
		return accountId;
	}

	public void setAccountId(String[] accountId) {
		this.accountId = accountId;
	}

	public String[] getDeadline() {
		return deadline;
	}

	public void setDeadline(String[] deadline) {
		this.deadline = deadline;
	}

	public String[] getWork() {
		return work;
	}

	public void setWork(String[] work) {
		this.work = work;
	}

	public String[] getPhone() {
		return phone;
	}

	public void setPhone(String[] phone) {
		this.phone = phone;
	}

	public String[] getAddinfo() {
		return addinfo;
	}

	public void setAddinfo(String[] addinfo) {
		this.addinfo = addinfo;
	}
}
